﻿using System;
using System.Collections;
using System.Data;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using Microsoft.SharePoint;
using Truextend.CoffeShop.Presenter.Orders;

namespace Truextend.CoffeShop.NewGrupalOrderRegistration
{
    public partial class NewGrupalOrderRegistrationUserControl : UserControl, IRegisterOrder
    {

        private static RegisterOrderPresenter presenter = null;

        protected void Page_Load(object sender, EventArgs e)
        {            
            HideMessageErrors();
            presenter = new RegisterOrderPresenter(this);

            if (!IsPostBack)
            {                                
                presenter.FillRestaurants();
                presenter.FillDatesTimes();
            }
        }

        public void SetError(string messageError, ViewPanelError viewPanelError)
        {
            switch (viewPanelError)
            {
                case ViewPanelError.Title:
                    PanelTitleError.Visible = true;
                    LblTitleError.Text = messageError;
                    break;
                case ViewPanelError.Dates:
                    PanelDatesError.Visible = true;
                    LblDateError.Text = messageError;
                    break;
                case ViewPanelError.Restaurant:
                    PanelRestaurantError.Visible = true;
                    LblRestaurantError.Text = messageError;
                    break;
                case ViewPanelError.Options:
                    PanelOptionError.Visible = true;
                    LblOptionError.Text = messageError;
                    break;
                case ViewPanelError.DeleveryTime:
                    PanelDeleveryTimeError.Visible = true;
                    LblDeleveryTimeError.Text = messageError;
                    break;
                default:
                    break;
            }
        }

        private void HideMessageErrors()
        {
            PanelTitleError.Visible = false;
            PanelRestaurantError.Visible = false;
            PanelOptionError.Visible = false;
            PanelDatesError.Visible = false;
            PanelDeleveryTimeError.Visible = false;
        }
        
        #region Implements IRegisterOrder

        public int userId
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.ID;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string userName
        {
            get
            {
                return SPContext.Current.Web.CurrentUser.LoginName;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public string title
        {
            get
            {
                return TxBxTitle.Text;
            }
            set
            {
                TxBxTitle.Text = value;
            }
        }

        public DateTime expectedDeliveryDate
        {
            get
            {
                return DTExpectedDeleveryDate.SelectedDate;
            }
            set
            {
                DTExpectedDeleveryDate.SelectedDate = value;
                DTStartRequest.MinDate = this.endRequestDate;
            }
        }

        public DateTime startRequest
        {
            get
            {
                return DTStartRequest.SelectedDate;
            }
            set
            {
                DTStartRequest.SelectedDate = value;
                DTStartRequest.MinDate = DateTime.Now;
            }
        }

        public DateTime endRequestDate
        {
            get
            {
                return DTEndDeliveryDate.SelectedDate;
            }
            set
            {
                DTEndDeliveryDate.SelectedDate = value;
                DTEndDeliveryDate.MinDate = this.startRequest;
            }
        }

        public System.Data.DataTable options
        {
            get
            {
                DataTable dataTable = new DataTable();
                dataTable.Columns.Add("OptionID", typeof(int));

                for (int i = 0; i < OptionsGridView.Rows.Count; i++)
                {
                    GridViewRow option_row = this.OptionsGridView.Rows[i];
                    CheckBox isChecked = option_row.FindControl("CheckBoxOption") as CheckBox;

                    if (isChecked.Checked)
                    {
                        DataRow row = dataTable.NewRow();
                        row["OptionID"] = int.Parse(this.OptionsGridView.DataKeys[i].Value.ToString());
                        dataTable.Rows.Add(row);
                    }
                }
                return dataTable;
            }
            set
            {
                OptionsGridView.DataSource = value;
                OptionsGridView.Columns[0].Visible = false;
                OptionsGridView.DataBind();
            }
        }

        public bool drink
        {
            get
            {
                return ChBxDrink.Checked;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        public Hashtable listRestaurants
        {
            get
            {
                return (Hashtable)DropDownListRestaurant.DataSource;
            }
            set
            {
                DropDownListRestaurant.DataSource = value;
                DropDownListRestaurant.DataTextField = "Value";
                DropDownListRestaurant.DataValueField = "Key";
                DropDownListRestaurant.DataBind();
            }
        }

        public string restaurant
        {
            get
            {
                return DropDownListRestaurant.SelectedItem.Text;
            }
            set
            {
                throw new NotImplementedException();
            }
        }

        #endregion                                                

        protected void BtnCreate_Click(object sender, EventArgs e)
        {
            if (presenter.InsertOrder())
            {
                Response.Redirect("~/CoffeeShop/SitePages/Home.aspx");
            }            
        }        

        protected void BtnCancel_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CoffeeShop/SitePages/Home.aspx");
        }

        protected void DropDownListRestaurant_SelectedIndexChanged(object sender, EventArgs e)
        {
            DivOptions.Visible = true;
            //presenter.FillDatesTimes();
            presenter.FillOptions(DropDownListRestaurant.SelectedItem.Value);            
        }

        protected void LinkBtnNewProvider_Click(object sender, EventArgs e)
        {
            Response.Redirect("~/CoffeeShop/SitePages/NewProvider.aspx");
        }
        
    }
}
